from google.appengine.api import users
from ezhangbendef import *
from trans import *
import logging

class AddExpense(TransRequestHandler):
    def checkExpense(self, t):
        # TODO check expense value if in the table
        return True

    def post(self):
        trans = Transcation()
        logging.debug("add expense");
        if (self.checkAmount() == False):
            return
        trans.amountnum = self.amountnum
        trans.is_pay = True
        trans.owner = users.get_current_user()
        trans.notes = cgi.escape(self.request.get('notes'))
        trans.expense_catelog = cgi.escape(self.request.get('expense_catelog'))
        trans.expense_through = cgi.escape(self.request.get('expense_through'))
        trans.expense_to_whom = cgi.escape(self.request.get('expense_to_whom'))
#        logging.info("expense add amountf:%s catelog:%s through:%s for who:%s notes:%s"
#        % (trans.amountf, trans.expense_catelog,
#        trans.expense_through, trans.expense_to_whom, trans.notes))
        if (self.checkExpense(trans) == True):
            trans.put()
            self.redirect('/')
        else:
            self.redirect('/')
            logging.error("check Expense return false \nexpense %s add amount:%s catelog:%s through:%s for who:%s notes:%s"
                          % (trans.owner, trans.amountnum, trans.expense_catelog, trans.expense_through, trans.expense_to_whom, trans.notes))

class AddIncome(TransRequestHandler):
    def checkIncome(self, t):
        # TODO check it
        return True

    def post(self):
        trans = Transcation()
        if (self.checkAmount() == False):
            return
        income_src = cgi.escape(self.request.get('incomesource'))
        trans.owner = users.get_current_user()
        trans.is_pay = False
        trans.amountnum = self.amountnum
        trans.income_source = income_src
        trans.notes = cgi.escape(self.request.get('notes'))
        if (self.checkIncome(trans) == True):
            trans.put()
            self.redirect('/')
        else:
            self.response.out.write("Check trans Error")

